package org.jeecg.modules.wlhy.hycarrier.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.DictUtil;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.third.aliyunocr.AliyunOcrService;
import org.jeecg.modules.wlhy.customer.entity.HyCustomer;
import org.jeecg.modules.wlhy.hycarrier.entity.HyCarrier;
import org.jeecg.modules.wlhy.hycarrier.entity.HyCarrierBank;
import org.jeecg.modules.wlhy.hycarrier.service.IHyCarrierBankService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.wlhy.hycarrier.service.IHyCarrierService;
import org.jeecg.modules.wlhy.hyproject.service.IHyProjectService;
import org.jeecg.modules.wlhy.utils.BankTypeUtil;
import org.jeecg.modules.wlhy.utils.HyCfgUtil;
import org.jeecg.modules.wlhy.utils.UserUtil;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: hy_carrier_bank
 * @Author: jeecg-boot
 * @Date:   2022-08-05
 * @Version: V1.0
 */
@Api(tags="hy_carrier_bank")
@RestController
@RequestMapping("/hycarrier/hyCarrierBank")
@Slf4j
public class HyCarrierBankController extends JeecgController<HyCarrierBank, IHyCarrierBankService> {
	@Autowired
	private IHyCarrierBankService hyCarrierBankService;

	 @Autowired
	 private UserUtil userUtil;

	 @Autowired
	 private IHyCarrierService hyCarrierService;

	 @Autowired
	 private DictUtil dictUtil;

	 @Autowired
	 private IHyProjectService hyProjectService;

	 @Autowired
	 private AliyunOcrService aliyunOcrService;



	@ApiOperation(value="hy_carrier_bank-分页列表查询", notes="hy_carrier_bank-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(HyCarrierBank hyCarrierBank,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<HyCarrierBank> queryWrapper = QueryGenerator.initQueryWrapper(hyCarrierBank, req.getParameterMap());
		if(!userUtil.isSys()) {
			HyCustomer customer = userUtil.getCustomer();
			if (customer == null) {
				return Result.error("货主信息不存在");
			}
			queryWrapper.eq("customer_id", customer.getId());
		}



		Page<HyCarrierBank> page = new Page<HyCarrierBank>(pageNo, pageSize);
		IPage<HyCarrierBank> pageList = hyCarrierBankService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @ApiOperation(value="hy_carrier_bank-分页列表查询", notes="hy_carrier_bank-分页列表查询")
	 @GetMapping(value = "/all")
	 public Result<?> all(@RequestParam(name="carrierId", defaultValue="") String carrierId) {
		 if (StringUtils.isEmpty(carrierId)) {
			 return Result.error("carrierId不能为空");
		 }
		 QueryWrapper<HyCarrierBank> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("carrier_id",carrierId);
		 if (!userUtil.isSys()) {
			 HyCustomer customer = userUtil.getCustomer();
			 if (customer == null) {
				 return Result.error("货主信息不存在");
			 }
			 queryWrapper.eq("customer_id", customer.getId());
		 }
		 queryWrapper.orderByDesc("create_time");
		 List<HyCarrierBank> list = hyCarrierBankService.list(queryWrapper);
		 List<?> ret = dictUtil.parseArrayListDict(list);
		 return Result.OK(ret);
	 }

	@AutoLog(value = "hy_carrier_bank-添加")
	@ApiOperation(value="hy_carrier_bank-添加", notes="hy_carrier_bank-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody HyCarrierBank hyCarrierBank) {

		String carrierId = hyCarrierBank.getCarrierId();
		if(StringUtils.isEmpty(carrierId)){
			return Result.error("车队长不能为空");
		}
		HyCarrier carrier = hyCarrierService.getById(carrierId);
		if(carrier == null){
			return Result.error("车队长信息不存在");
		}
		if(!userUtil.isSys()){
			HyCustomer customer = userUtil.getCustomer();
			if(customer == null){
				return Result.error("货主信息不存在");
			}
			if(!StringUtils.equals(customer.getId() , carrier.getCustomerId())){
				return Result.error("车队长信息不存在");
			}
		}
		hyCarrierBank.setBankNum(hyCarrierBank.getBankNum().replaceAll(" ",""));
		hyCarrierBank.setCustomerId(carrier.getCustomerId());

		if(HyCfgUtil.isUsed2("checkBankCard")){
			if(StringUtils.isEmpty(hyCarrierBank.getIdcard())){
				return Result.error("开户人身份证号不能为空");
			}
			//验证银行卡三要素
			Result<?> result = aliyunOcrService.ToolsBank(hyCarrierBank.getIdcard(), hyCarrierBank.getBankName(), hyCarrierBank.getBankNum());
			if(!result.isSuccess()){
				return result;
			}
			JSONObject ret = (JSONObject) result.getResult();
			String bank = ret.getString("bank");
			String bankType = BankTypeUtil.getBankTypeByBankName2(bank);
			if(StringUtils.isEmpty(bankType)){
				return Result.error("未找到对应的银行卡类别,请联系管理员,银行名称:" + bank);
			}
			hyCarrierBank.setBankType(bankType);
		}else {
			String bankType = hyCarrierBank.getBankType();
			if(StringUtils.isEmpty(bankType)){
				return Result.error("银行类别不能为空");
			}
			if(!BankTypeUtil.checkBankType(bankType)){
				return Result.error("银行类别错误");
			}
		}
		hyCarrierBankService.save(hyCarrierBank);
		return Result.OK("添加成功！");
	}

	@AutoLog(value = "hy_carrier_bank-编辑")
	@ApiOperation(value="hy_carrier_bank-编辑", notes="hy_carrier_bank-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody HyCarrierBank hyCarrierBank) {
		if(!userUtil.isSys()){
			HyCustomer customer = userUtil.getCustomer();
			if(customer == null){
				return Result.error("货主信息不存在");
			}
			if(!StringUtils.equals(customer.getId() , hyCarrierBank.getCustomerId())){
				return Result.error("银行卡信息错误");
			}
		}
		hyCarrierBank.setBankNum(hyCarrierBank.getBankNum().replaceAll(" ",""));

		if(HyCfgUtil.isUsed2("checkBankCard")){
			if(StringUtils.isEmpty(hyCarrierBank.getIdcard())){
				return Result.error("开户人身份证号不能为空");
			}
			//验证银行卡三要素
			Result<?> result = aliyunOcrService.ToolsBank(hyCarrierBank.getIdcard(), hyCarrierBank.getBankName(), hyCarrierBank.getBankNum());
			if(!result.isSuccess()){
				return result;
			}
			JSONObject ret = (JSONObject) result.getResult();
			String bank = ret.getString("bank");
			String bankType = BankTypeUtil.getBankTypeByBankName2(bank);
			if(StringUtils.isEmpty(bankType)){
				return Result.error("未找到对应的银行卡类别,请联系管理员,银行名称:" + bank);
			}
			hyCarrierBank.setBankType(bankType);
		}else {
			String bankType = hyCarrierBank.getBankType();
			if(StringUtils.isEmpty(bankType)){
				return Result.error("银行类别不能为空");
			}
			if(!BankTypeUtil.checkBankType(bankType)){
				return Result.error("银行类别错误");
			}
		}

		hyCarrierBankService.updateById(hyCarrierBank);
		return Result.OK("编辑成功!");
	}

	@AutoLog(value = "hy_carrier_bank-通过id删除")
	@ApiOperation(value="hy_carrier_bank-通过id删除", notes="hy_carrier_bank-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		hyCarrierBankService.removeById(id);
		return Result.OK("删除成功!");
	}
//
//	/**
//	 *  批量删除
//	 *
//	 * @param ids
//	 * @return
//	 */
//	@AutoLog(value = "hy_carrier_bank-批量删除")
//	@ApiOperation(value="hy_carrier_bank-批量删除", notes="hy_carrier_bank-批量删除")
//	@DeleteMapping(value = "/deleteBatch")
//	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
//		this.hyCarrierBankService.removeByIds(Arrays.asList(ids.split(",")));
//		return Result.OK("批量删除成功!");
//	}
//
//	/**
//	 * 通过id查询
//	 *
//	 * @param id
//	 * @return
//	 */
//	@ApiOperation(value="hy_carrier_bank-通过id查询", notes="hy_carrier_bank-通过id查询")
//	@GetMapping(value = "/queryById")
//	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
//		HyCarrierBank hyCarrierBank = hyCarrierBankService.getById(id);
//		if(hyCarrierBank==null) {
//			return Result.error("未找到对应数据");
//		}
//		return Result.OK(hyCarrierBank);
//	}
//
//    /**
//    * 导出excel
//    *
//    * @param request
//    * @param hyCarrierBank
//    */
//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletRequest request, HyCarrierBank hyCarrierBank) {
//        return super.exportXls(request, hyCarrierBank, HyCarrierBank.class, "hy_carrier_bank");
//    }
//
//    /**
//      * 通过excel导入数据
//    *
//    * @param request
//    * @param response
//    * @return
//    */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, HyCarrierBank.class);
//    }

}
